﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using SQLServerHealthCheck.Model;

namespace SQLServerHealthCheck.SQLServer.IssueTypes
{
    public class ServerCheckFreeSpaceLess10percent:IssueTypeCheckBase
    {
        public ServerCheckFreeSpaceLess10percent()
        {
            IssueTypeId = 1001;
        }

        public override void Check()
        {
            HealthCheckDataContext dc = new HealthCheckDataContext(System.Configuration.ConfigurationManager.AppSettings["ConnectionStringSSHCDatabase"]);
            Issue issue;

            var ServerQry = from sd in dc.Servers
                            from d in dc.ServerDrives
                           
                            where d.ServerID == sd.ServerID                            

                            where (Convert.ToDouble((d.FreeSpaceInGB / d.TotalSpaceInGB)) <  0.10)


                            select d;
                          
             foreach (var item in ServerQry)
            {
             
                //Get Issuetype object

                //Get Issuetype object  And Create a new  Global issue object 
               
                 IssueType issueType = dc.IssueTypes.FirstOrDefault(i => i.IssueTypeId == IssueTypeId);

                issue = new Issue(); 
           
                issue.IssueType = issueType;
                issue.TargetId = item.ServerID;
                issue.Description = issueType.Description.Replace("{ServerName}", item.Server.Name).Replace("{Drive}", item.Drive.ToString());

                dc.Issues.InsertOnSubmit(issue);
            }

            dc.SubmitChanges();
        }


    }
        
        
      
    
    
    
    
    
}
